package cn.ztydata.teachingsystem.heaven.common;

import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.entity.oxm.PrivilegeInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.Unmarshaller;

import javax.servlet.ServletContext;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;

/**
 * 权限加载工厂类
 * Created by chenxin on 2015/2/28.
 */
public class PrivilegeLoaderFactory {
    private static final Logger log = LoggerFactory.getLogger(PrivilegeLoaderFactory.class); //日志记录器

    private Unmarshaller xstreamMarshaller; //xml解析类

    /**
     * 加载权限
     * @param role
     * @param servletContext
     * @return 权限信息
     * @throws IOException
     *
     * @author cx
     * @since 2015/02/28
     */
    public PrivilegeInfo load(final int role, ServletContext servletContext) throws IOException {
        String prefix = "privilege/";
        switch (role) {
            case Roles.COLLEGE_USER : //院级用户
                log.debug("院级用户:Roles.COLLEGE_USER");
                return readPrivileges(prefix + "college_user_privilege.xml", "college_user", servletContext);
            case Roles.DEPT_ADMIN : //系部管理员
                log.debug("系部管理员:Roles.COLLEGE_USER");
                return readPrivileges(prefix + "dept_admin_privilege.xml", "dept_admin", servletContext);
            case Roles.SPEC_LEADER : //专业负责人
                log.debug("专业负责人:Roles.SPEC_LEADER");
                return readPrivileges(prefix + "spec_leader_privilege.xml", "spec_leader", servletContext);
            case Roles.TEACHER : //专业教师
                log.debug("专业教师:Roles.TEACHER");
                return readPrivileges(prefix + "teacher_privilege.xml", "teacher", servletContext);
            case Roles.DEFAULT : //默认权限
                log.debug("默认权限:Roles.TEACHER");
                return readPrivileges(prefix + "default_privilege.xml", "default", servletContext);
            case Roles.STUDENT: //学生权限
                log.debug("学生权限:Roles.STUDENT");
                return readPrivileges(prefix + "student_privilege.xml", "student", servletContext);
            default:
                log.debug("error role");
                return null;
        }
    }


    /**
     * Xml映射为对象
     * @param fileName 权限配置文件
     * @return privilegeInfo 权限信息
     *
     * @author chenxin
     * @since 2015/02/28
     */
    private PrivilegeInfo readFromXml(final String fileName) throws IOException {
        //读取类路径下的权限xml文件
        Resource resource = new ClassPathResource(fileName);

        //获取权限文件输入流
        InputStream in = resource.getInputStream();
        //将xml转为对象
        PrivilegeInfo privilegeInfo = (PrivilegeInfo) xstreamMarshaller.unmarshal(new StreamSource(in));
        //关闭输入流
        in.close();

        log.debug("xml映射对象");
        return privilegeInfo;
    }

    /**
     * 读取权限
     * @param fileName 权限配置文件
     * @param keyName context的键名
     * @param servletContext web上下文
     * @return privilegeInfo 权限信息
     * @throws IOException IO异常
     *
     * @author chenxin
     * @since 2015/2/28
     */
    private PrivilegeInfo readPrivileges(final String fileName, final String keyName, ServletContext servletContext) throws IOException {
        //从ServletContext加载权限
        PrivilegeInfo privilegeInfo = (PrivilegeInfo) servletContext.getAttribute(keyName);

        if (privilegeInfo == null) {
            log.debug("privilegeInfo == null");

            //从权限配置文件加载权限
            privilegeInfo = readFromXml(fileName);
            //将读取的权限信息存入ServletContext
            servletContext.setAttribute(keyName, privilegeInfo);
        }

        log.debug("读取权限");
        return privilegeInfo;
    }

    public Unmarshaller getXstreamMarshaller() {
        return xstreamMarshaller;
    }

    public void setXstreamMarshaller(Unmarshaller xstreamMarshaller) {
        this.xstreamMarshaller = xstreamMarshaller;
    }
}
